<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="任务标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入任务标题" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="isCompleted">
        <el-select v-model="queryParams.isCompleted" placeholder="任务状态" clearable style="width: 200px">
          <el-option label="未完成" :value="false" />
          <el-option label="已完成" :value="true" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="Sort" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="todoList" row-key="id">
      <el-table-column prop="title" label="任务标题" width="260"></el-table-column>
      <el-table-column prop="task" label="任务内容" width="300"></el-table-column>
      <el-table-column prop="dueDate" label="截止日期" width="200">
        <template #default="scope">
          <span>{{ formatDate(scope.row.dueDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="priority" label="优先级" width="100">
        <template #default="scope">
          <span>{{ scope.row.priority }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改待办事项对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="todoRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入任务标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务内容" prop="task">
              <el-input v-model="form.task" placeholder="请输入任务内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截止日期" prop="dueDate">
              <el-date-picker v-model="form.dueDate" type="datetime" placeholder="选择截止日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="优先级" prop="priority">
              <el-select v-model="form.priority" placeholder="选择优先级">
                <el-option label="低" value="low" />
                <el-option label="中" value="medium" />
                <el-option label="高" value="high" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.isCompleted">
                <el-radio :label="false">未完成</el-radio>
                <el-radio :label="true">已完成</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TodoList">
import { ref, reactive } from 'vue';
import { listTodos, addTodo, updateTodo, deleteTodo } from "@/api/pages/todo";

const loading = ref(true);
const open = ref(false);
const showSearch = ref(true);
const title = ref("");
const refreshTable = ref(true);
const todoList = ref([]);
const form = reactive({
  id: undefined,
  title: "",
  task: "",
  isCompleted: false,
  dueDate: "",
  priority: "medium"
});
const queryParams = reactive({
  title: "",
  isCompleted: undefined
});
const rules = {
  title: [{ required: true, message: "任务标题不能为空", trigger: "blur" }],
  task: [{ required: true, message: "任务内容不能为空", trigger: "blur" }],
  dueDate: [{ required: true, message: "截止日期不能为空", trigger: "change" }]
};

// 查询待办事项列表
const getList = () => {
  loading.value = true;
  listTodos(queryParams).then(response => {
    todoList.value = response.data;
    loading.value = false;
  });
};

// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};

// 重置表单
const reset = () => {
  Object.assign(form, {
    id: undefined,
    title: "",
    task: "",
    isCompleted: false,
    dueDate: "",
    priority: "medium"
  });
};

// 搜索按钮操作
const handleQuery = () => {
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  Object.assign(queryParams, {
    title: "",
    isCompleted: undefined
  });
  getList();
};

// 新增按钮操作
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "新增待办事项";
};

// 修改按钮操作
const handleUpdate = (row) => {
  Object.assign(form, row);
  open.value = true;
  title.value = "修改待办事项";
};

// 提交按钮
const submitForm = () => {
  const apiCall = form.id ? updateTodo : addTodo;
  apiCall(form).then(() => {
    getList();
    open.value = false;
  });
};

// 删除按钮操作
const handleDelete = (row) => {
  deleteTodo(row.id).then(() => {
    getList();
  });
};

// 格式化日期
const formatDate = (date) => {
  return new Date(date).toLocaleString();
};

// 初始化待办事项列表
getList();
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.dialog-footer {
  text-align: right;
}
</style>